<?php


namespace app\admin\controller;

use app\admin\facade\JwtAuth;
use  app\admin\validate\Login AS LoginV;
use  app\admin\model\User AS UserM;
use  app\admin\service\User AS UserS;
use app\admin\model\Route as RouteM;
use app\admin\model\Role as RoleM;

class Login extends Base
{
    protected $not_need_login_action = ['in','out'];

    /**
     * Desc: 登录
     * User: 苏鹏
     * Date: 2023/9/7
     * Time: 14:08
     * @return void
     */
    public function in()
    {

        $account = input('account');
        $password = input('password');
        $is_menu = input('is_menu', 1, 'intval'); //是否返回菜单 默认返回

        try{
            //验证数据
            validate(LoginV::class)->check([
                'account' => $account,
                'password' => $password,
            ]);
            //验证密码,成功生成token，失败返回错误码
            $userM = new UserM();
            $user_res = $userM->where('account', $account)->where('status','<>', 3)->field('id,salt,nickname,account,status')->find();
            if (!empty($user_res))
            {
                $user_res = $user_res->toArray();
            }else {
                return re_error_json(msg_code(306)); //账号或密码错误
            }

            //验证用户状态
            if($user_res['status'] == 2)
            {
                return re_error_json(msg_code(15)); //用户被禁用，请联系管理员
            }
            //验证密码
            if (verify_password($password, $user_res['salt']))
            {
                //生成token
                $token = JwtAuth::signToken($user_res);
                $user_res['token'] = $token;
                unset($user_res['salt']);
                $user_res['menu'] = [];
                $this->user_id = $user_res['id'];
                request()->user_id = $user_res['id'];
                $userS = new UserS();
                $userS->tokenInfoInRedis('set', $user_res);
                //将用户在有效期的token全部加入黑名单，并清空关联关系
                $userS->clearTokenAndJoinBlacklist($user_res['id']);

                $userS->userReTokenInRedis('set', $user_res['id'], $token); //将新的token用户与用户管理

                //返回后台侧边栏菜单
                if($is_menu == 1){
                    //是否是超级管理员,超级管理员返回所有权限
                    $admin_ids = get_config_cache('super_admin_ids');
                    if(in_array($this->user_id, $admin_ids))
                    {
                        $route_list = (new RouteM())->getRouteTreeLists(1);
                    }else{
                        //非管理员根据角色返回权限
                        $route_list = (new RoleM)->getRoleReRouteTreeLists($this->user_id);
                    }
                    $user_res['menu'] = $route_list;
                }

                return re_success_json($user_res);
            }
            return re_error_json(msg_code(306)); //账号或密码错误
        }catch (\Exception $e){
            return re_error_json($e->getMessage());
        }

    }

    /**
     * Desc: 退出登录
     * User: 苏鹏
     * Date: 2023/9/7
     * Time: 14:59
     * @return void
     */
    public function out()
    {

    }

}